package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.logic.AlertLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.MsgTipsLogic;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.loader.SwfLoader;
	import newx.utils.FilterUtil;

	public class WlybGetLogic
	{
		
		private var _ui:WlybGetUI;
		public static var _self:WlybGetLogic;
		/**数据模型*/
		private 	var _model			:ModelLocator;
		
		/**绑定领取次数**/
		private var bindCountIndex:int = -1;
		
		private var N:int = 10;
		
		private var _count : int;
		
		private var _reward : int;
		private var _allCount:int;
		private var _allReward:int;
		
		private var _msgtip:MsgTipsLogic;
		/***活动图标界面**/
		private var _activitesGroup:ActivitesGroupLogic;
		public function WlybGetLogic()
		{
			_activitesGroup = ActivitesGroupLogic.getInstance();	
		}
		public static function getInstance():WlybGetLogic
		{
			if(_self == null)
			{
				_self =new WlybGetLogic();
			}
			return _self;
		}
		
		
		public function uiinit():void
		{
			_model = ModelLocator.getInstance();
			
			_model.addEventListener( String(CustomToClientFlag.SERVER_CUSTOMMSG_WLYB_REWARD), changeReward );
			_model.addEventListener( String(CustomToClientFlag.SERVER_CUSTOMMSG_WLYB_GET_SAVVY), getSavvyHandle );
		}
		
		private function getSavvyHandle(e:CustomMsgEvent):void
		{
			 if (e.pList[1] == 1)
			 {
				 AlertUI.showAlert("",StaticIni.getSwfLangStrVar("UIString_1000739",[e.pList[0]]),AlertUI.OK,restBtn);
				 
			 }else
			 {
				 _msgtip.showInfo(StaticIni.getSwfLangStrVar("UIString_1000469",[e.pList[0]]));
			 }
			
		}
		private function restBtn(type:int, str:String, clickData:*):void
		{
			if(AlertUI.OK == type )
			{
				
			}
		}
		private function changeReward( evt : CustomMsgEvent ):void
		{
			_count = evt.pList[ 0 ];
			_reward = evt.pList[ 1 ];
			_allCount = evt.pList[2];
			_allReward = evt.pList[3];
			updata ( _model.scene.Role.GetPropertiesEx( "WLYBCanRewardTimes" ) );
		}
		
		private function onClickCloseHandle(e:NxWindowEvent):void
		{
			ui.hide();
		}
		
		
		private function onGetClickHandle(e:MouseEvent):void
		{
			_msgtip = MsgTipsLogic.getRes(e.currentTarget as DisplayObjectContainer);
			SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_GET_WLYB_REWARD,1 );
		}
		
		private function onGetOverHandle(e:MouseEvent):void
		{
			var N:int = 10;
			var WLYBCurFloor:int = _model.scene.Role.GetPropertiesEx("WLYBCurFloor");
			var WLYBDefLevel:int = _model.scene.Role.GetPropertiesEx("WLYBDefLevel");
			var countNum:int = N*(WLYBCurFloor + WLYBDefLevel); 
			var times:int = _model.scene.Role.GetProperties("WLYBRewardGetTimes");
//			TipsMainLogic.instance.showMain(StaticIni.getSwfLangStrVar("UIString_1000394",[getCoin(  times ) , countNum ]) );
		}
		
		private function onGetOutHandle(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();	
		}
		
		private function onGetallClickHandle(e:MouseEvent):void
		{
			_msgtip = MsgTipsLogic.getRes(e.currentTarget as DisplayObjectContainer);
			SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_GET_WLYB_REWARD,10 );
		}
		
		private function onGetallOverHandle(e:MouseEvent):void
		{
			TipsMainLogic.instance.showMain( StaticIni.getSwfLangStrVar("UIString_1000394",[_allCount ,_allReward]) );
		}
		
		private function updata(value:int):void
		{
			ui.lab_count.htmlStr = StaticIni.getSwfLangStrVar("UIString_1000192",[value]);
			var gettimes:int = _model.scene.Role.GetPropertiesEx("WLYBRewardGetTimes");
			if(_count<=0)
			{
				ui.lab_info.htmlStr = StaticIni.getSwfLangStrVar("UIString_1000392",[_reward]);
			}else
			{
				ui.lab_info.htmlStr = StaticIni.getSwfLangStrVar("UIString_1000393",[_count,_reward]);
			}
			
			if(_allCount == -1)
			{
				ui.btn_getall.filters = [FilterUtil.GrayColorFilter];
				ui.btn_getall.removeEventListener(MouseEvent.MOUSE_OVER,onGetallOverHandle);
				ui.btn_getall.removeEventListener(MouseEvent.MOUSE_OUT,onGetOutHandle);
			}else
			{
				ui.btn_getall.filters = [];
				ui.btn_getall.addEventListener(MouseEvent.MOUSE_OVER,onGetallOverHandle);
				ui.btn_getall.addEventListener(MouseEvent.MOUSE_OUT,onGetOutHandle);
			}
			
			var vipLevel:int = _model.scene.Role.getVipLevel();
			var needLevel:int = int(StaticIni.getIniVar("wlybutil","vipLevel"));
			if(vipLevel>=needLevel)
			{
				ui.btn_get.visible = true;
				ui.btn_getall.visible = true;
				ui.btn_get.x = 32;
				ui.btn_getall.x = 210;
			}else
			{
				ui.btn_getall.visible = false;
				ui.btn_get.visible = true;
				ui.btn_get.x = 120;
			}
			if(value<=0)
			{
				ui.btn_get.enabled = false;
				ui.btn_getall.enabled = false;
			}else
			{
				ui.btn_get.enabled = true;
				ui.btn_getall.enabled = true;
			}
		}
		
		public  function  set onCountChange(value:int):void
		{
			if(value<=0)
			{
				ui.btn_get.enabled = false;
				ui.btn_getall.enabled = false;
			}
			SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_WLYB_REWARD_UI );
		}
		
		public function isShowView():Boolean
		{
			var WLYBCurFloor:int = _model.scene.Role.GetPropertiesEx("WLYBCurFloor");
			var WLYBDefLevel:int = _model.scene.Role.GetPropertiesEx("WLYBDefLevel");
			
			var countNum:int = N*(WLYBCurFloor+WLYBDefLevel); 
			var WLYBCanRewardTimes:int = _model.scene.Role.GetPropertiesEx("WLYBCanRewardTimes");
			if(countNum <= 0 || WLYBCanRewardTimes <= 0  )
			{
				return false
			}
			
			return true;
			
		}
	
		
		private function setUIview():void
		{
			bindCountIndex = BindManage.getInstance().BindProp(_model.scene.Role,"WLYBCanRewardTimes",this,"onCountChange");
		
		}
		
		/**
		 *添加监听 
		 * 
		 */
		private function addListenter():void
		{
			ui.windowBG	.addEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
			
			ui.btn_get.addEventListener(MouseEvent.CLICK,onGetClickHandle);
//			ui.btn_get.addEventListener(MouseEvent.MOUSE_OVER,onGetOverHandle);
			ui.btn_get.addEventListener(MouseEvent.MOUSE_OUT,onGetOutHandle);
			
			ui.btn_getall.addEventListener(MouseEvent.CLICK,onGetallClickHandle);
			ui.btn_getall.addEventListener(MouseEvent.MOUSE_OVER,onGetallOverHandle);
			ui.btn_getall.addEventListener(MouseEvent.MOUSE_OUT,onGetOutHandle);
		}
		
		/**
		 *移除监听 
		 * 
		 */
		private  function removeListenter():void
		{
			ui.windowBG	.removeEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
			
			
			ui.btn_get.removeEventListener(MouseEvent.CLICK,onGetClickHandle);
//			ui.btn_get.removeEventListener(MouseEvent.MOUSE_OVER,onGetOverHandle);
			ui.btn_get.removeEventListener(MouseEvent.MOUSE_OUT,onGetOutHandle);
			
			ui.btn_getall.removeEventListener(MouseEvent.CLICK,onGetallClickHandle);
			ui.btn_getall.removeEventListener(MouseEvent.MOUSE_OVER,onGetallOverHandle);
			ui.btn_getall.removeEventListener(MouseEvent.MOUSE_OUT,onGetOutHandle);
		}
		
		
		public function BeforeShow():void
		{
//			SwfLoader.StartLoad(ResPath.SWF + "wlybget.swf", null, loadover);
			ResManager.loadRes( "wlybget", ResPath.SWF + "wlybget.swf", onComplete );
			
			function onComplete(swf:DisplayObject,pList:Array = null):void
			{
				var mc : MovieClip = ResManager.newResClass( "wlybget") as MovieClip;
				ui.pic.source = mc;
			}
//			function loadover(mc:MovieClip,...args):void
//			{
//				ui.pic.source = mc;
//			}
			
			setUIview();
			addListenter();
			
//			SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_WLYB_REWARD_UI );
		}
		
		public function AfterHide():void
		{
			BindManage.getInstance().UnbindProp(_model.scene.Role,"WLYBCanRewardTimes",bindCountIndex);
			removeListenter();
		}
		
		public function get ui():WlybGetUI
		{
			if(_ui==null)
			{
				_ui	=	new WlybGetUI();
			}
			return _ui
		}
	}
}